﻿using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using ManagerClient.AuthentificationService;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Win32;

namespace ManagerClient.Manage.Logic
{
    public class CreateDishControlVM : ViewModelBase
    {
        private readonly AuthentificationServiceClient client;
        private double? calorificEffect;
        private byte[] imageContent;
        private string name;
        private double price;
        private string structure;

        public CreateDishControlVM(AuthentificationServiceClient client)
        {
            LoadImageCommand = new DelegateCommand(LoadImage);
            SaveCommand = new DelegateCommand(Save);
            this.client = client;
        }

        [Required(ErrorMessage = @"Поле ""Название"" необходимо заполнить")]
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                RaisePropertyChanged(() => Name);
            }
        }

        public double? CalorificEffect
        {
            get { return calorificEffect; }
            set
            {
                calorificEffect = value;
                RaisePropertyChanged(() => CalorificEffect);
            }
        }

        public string Structure
        {
            get { return structure; }
            set
            {
                structure = value;
                RaisePropertyChanged(() => Structure);
            }
        }

        [Required(ErrorMessage = @"Поле ""Цена"" необходимо заполнить")]
        public double Price
        {
            get { return price; }
            set
            {
                price = value;
                RaisePropertyChanged(() => Price);
            }
        }

        public DelegateCommand SaveCommand { get; set; }

        public byte[] ImageContent
        {
            get { return imageContent; }
            set
            {
                imageContent = value;
                RaisePropertyChanged(() => ImageContent);
            }
        }

        public DelegateCommand LoadImageCommand { get; set; }

        public string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }


        public string Error { get; private set; }


        protected virtual void OnSaved(Saved e)
        {
            EventHandler<Saved> handler = Saved;
            if (handler != null) handler(this, e);
        }


        private void Save()
        {
            var dish = new Dish
                {
                    Name = Name,
                    Structure = Structure,
                    CalorificEffect = CalorificEffect,
                    Price = Price,
                    ProviderId = 1,
                    Image = imageContent
                };
            IsBusy = true;

            OnSaved(new Saved {IsSaved = true, Dish = dish});
        }


        private void LoadImage()
        {
            var dlg =
                new OpenFileDialog();
            dlg.ShowDialog();

            var fs = new FileStream(dlg.FileName, FileMode.Open,
                                    FileAccess.Read);

            imageContent = new byte[fs.Length];
            fs.Read(imageContent, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            ImageContent = imageContent;
        }

        public event EventHandler<Saved> Saved;
    }

    public class Saved : EventArgs
    {
        public bool IsSaved { get; set; }

        public Dish Dish { get; set; }
    }
}